﻿using System;

namespace Rosetta.Default
{
    public abstract class Inputter : AddIn, IInputter
    {
        #region Constructors

        protected Inputter(string name)
            : base(name, Validate)
        {
        }

        #endregion

        #region Properties

        public Type ContextType { get; protected set; }

        public Type[] ReturnTypes
        {
            get
            {
                return ReturnTypesInternal;
            }
        }

        #endregion

        #region Public Methods

        public TResult Get<TResult>(string name)
        {
            return ((Func<TResult>)this[name])();
        }

        public virtual void UseContext(object context)
        {
        }

        #endregion

        #region Protected Methods

        protected void Add<TResult>([NotNull] string name, [NotNull] Func<TResult> handler)
        {
            AddMethod(name, handler, null);
        }

        #endregion

        #region Class Methods

        private static bool Validate([CanBeNull] Delegate handler)
        {
            if (handler != null)
            {
                var type = handler.GetType();

                return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Func<>);
            }

            return false;
        }

        #endregion
    }
}